# Copyright (c) 2008-2009 Mikhail Chernov 
#
# This file is part of dcExplorer.
#
# dcExplorer is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 2 of the License, or
# (at your option) any later version.
#
# dcExplorer is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with dcExplorer.  If not, see <http://www.gnu.org/licenses/>.

package dcExplorer::loader::IncrementalDB;
use strict;
use loader::Generic;
use base 'dcExplorer::loader::Generic';

use DBI;

sub loadPlayerTurn
{
	my $self = shift;
	my $dbh = shift;
	my $turn = shift;
	my $id = $self->{Empire}->getProp('player_id');

	$self->{Empire}->setProp('time_left', 0);
	
	my $sth = $dbh->prepare("select * from player_turn where reporter_id = ".$id.(defined($turn) ? ' and turn <= '.$turn : '')." order by turn desc limit 1");
	$sth->execute();
	if (my $ref = $sth->fetchrow_hashref())
	{
		for my $key (keys %{$ref})
		{
			$self->{Empire}->setProp($key, $ref->{$key}) if $key ne 'reporter_id';
		}
	}
	$sth->finish();
}

sub loadPlayerRace
{
	my $self = shift;
	my $dbh = shift;
	my $turn = $self->{Empire}->getProp('turn');
	my $id = $self->{Empire}->getProp('player_id');
	my $sth = $dbh->prepare("select * from player_race, race where player_race.reporter_id = ".$id." and player_race.player_id = ".$id." and player_race.race_id = race.id");
	$sth->execute();
	if (my $ref = $sth->fetchrow_hashref())
	{
		$self->{Empire}->setProp('race_name', $ref->{'name'});
		$self->{Empire}->setProp('t_delta', $ref->{'t_delta'} / 100);
		for my $key (qw(race_id t_optimal race_nature industry_nature unused_resource zanud spec nomads longlife intell old traiders small))
		{
			$self->{Empire}->setProp($key, $ref->{$key});
		}
		my @bonuses = qw(bonus_multiply bonus_speed bonus_dr bonus_price bonus_iq bonus_build_peace bonus_build_war bonus_hp bonus_attack bonus_mining bonus_stealth bonus_detectors);
		for my $key (@bonuses)
		{
			$self->{Empire}->setProp($key, $ref->{$key} / 10);
		}
	}
	$sth->finish();
}

sub loadPlanetsGeo 
{
	my $self = shift;
	my $dbh = shift;
	my $turn = $self->{Empire}->getProp('turn');
	my $id = $self->{Empire}->getProp('player_id');

	my $sth = $dbh->prepare("select x, y, o, e, m, t, s from planet_geo where reporter_id = ".$id.(defined($turn) ? ' and turn <= '.$turn : '')." order by turn");
	$sth->execute();
	while (my $ref = $sth->fetchrow_hashref())
	{
		my $planet = $self->{Empire}->planets->get($ref->{'x'}, $ref->{'y'});
		$planet->setProp($_, $ref->{$_}) foreach qw(o e m t s);
		$self->{Empire}->planets->set($planet);
	}
	$sth->finish();
}

sub loadPlanetsOwners
{
	my $self = shift;
	my $dbh = shift;
	my $turn = $self->{Empire}->getProp('turn');
	my $id = $self->{Empire}->getProp('player_id');

	my $sth = $dbh->prepare("select x, y, turn, owner_id from planet_owner where reporter_id = $id".(defined $turn ? ' and turn <= '.$turn : '')." order by turn");
	$sth->execute();
	while (my $ref = $sth->fetchrow_hashref())
	{
		my $planet = $self->{Empire}->planets->get($ref->{'x'}, $ref->{'y'});
		$planet->setProp('owner_id', $ref->{'owner_id'});
		$planet->setProp('my', defined($ref->{'owner_id'}) && $ref->{'owner_id'} == $id);
		$self->{Empire}->planets->set($planet);
	}
	$sth->finish();
}

sub loadPlanetsJumpable
{
	my $self = shift;
	my $dbh = shift;
	my $turn = $self->{Empire}->getProp('turn');
	my $id = $self->{Empire}->getProp('player_id');

	my $sth = $dbh->prepare("select x, y, turn, jumpable from planet_jumpable where reporter_id = $id".(defined $turn ? ' and turn <= '.$turn : '')." order by turn");
	$sth->execute();
	while (my $ref = $sth->fetchrow_hashref())
	{
		my $planet = $self->{Empire}->planets->get($ref->{'x'}, $ref->{'y'});
		$planet->setProp('jumpable', $ref->{'jumpable'});
		$self->{Empire}->planets->set($planet);
	}
	$sth->finish();
}

sub loadPlanetsAge
{
	my $self = shift;
	my $dbh = shift;
	my $turn = $self->{Empire}->getProp('turn');
	my $id = $self->{Empire}->getProp('player_id');

	my $sth = $dbh->prepare("select x, y, turn, age from planet_age where reporter_id = $id".(defined $turn ? ' and turn <= '.$turn : '')." order by turn");
	$sth->execute();
	while (my $ref = $sth->fetchrow_hashref())
	{
		my $planet = $self->{Empire}->planets->get($ref->{'x'}, $ref->{'y'});
		$planet->setProp('age', $ref->{'age'});
		$self->{Empire}->planets->set($planet);
	}
	$sth->finish();
}

sub loadPlanetsName
{
	my $self = shift;
	my $dbh = shift;
	my $turn = $self->{Empire}->getProp('turn');
	my $id = $self->{Empire}->getProp('player_id');

	my $sth = $dbh->prepare("select x, y, turn, name from planet_name where reporter_id = $id".(defined $turn ? ' and turn <= '.$turn : '')." order by turn");
	$sth->execute();
	while (my $ref = $sth->fetchrow_hashref())
	{
		my $planet = $self->{Empire}->planets->get($ref->{'x'}, $ref->{'y'});
		$planet->setProp('name', $ref->{'name'});
		$self->{Empire}->planets->set($planet);
	}
	$sth->finish();
}

sub loadDipRelations
{
	my $self = shift;
	my $dbh = shift;
	my $turn = $self->{Empire}->getProp('turn');
	my $id = $self->{Empire}->getProp('player_id');

	my $sth = $dbh->prepare("select player, type, turn from dip_relations where reporter_id = $id".(defined $turn ? ' and turn <= '.$turn : '')." order by turn");
	$sth->execute();
	while (my $ref = $sth->fetchrow_hashref())
	{
		$self->{Empire}->setDipRelation($ref->{'player'}, $ref->{'type'});
	}
	$sth->finish();
}

sub loadPlayers
{
	my $self = shift;
	my $dbh = shift;
	my $turn = $self->{Empire}->getProp('turn');
	my $id = $self->{Empire}->getProp('player_id');

	my $sth = $dbh->prepare("select player, name, turn from player_name where reporter_id = $id".(defined $turn ? ' and turn <= '.$turn : '')." order by turn");
	$sth->execute();
	while (my $ref = $sth->fetchrow_hashref())
	{
		my $player = $self->{Empire}->players->get($ref->{'player'});
		$player->setProp('name', $ref->{'name'});
		$self->{Empire}->players->set($player);
	}
	$sth->finish();

}

sub loadDB
{
	loadPlayerTurn(@_);
	loadPlayerRace(@_);
	loadPlanetsGeo(@_);
	loadPlanetsOwners(@_);
	loadPlanetsJumpable(@_);
	loadPlanetsAge(@_);
	loadPlanetsName(@_);
	loadDipRelations(@_);
	loadPlayers(@_);
}

1;
